package com.mm.cloth;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mm.cloth.adapter.CategoryAdapter;
import com.mm.cloth.adapter.ClothesAdapter;
import com.mm.cloth.model.Category;
import com.mm.cloth.model.Clothes;
import com.mm.cloth.model.Match;
import com.mm.cloth.utils.LoginCheckUtil;

import java.util.ArrayList;
import java.util.List;

public class AddMatchActivity extends AppCompatActivity {

    // UI组件
    private EditText etTitle;
    private EditText etDescription;
    private RecyclerView rvCategories;
    private RecyclerView rvSelectedClothes;
    private RecyclerView rvClothes;
    private Button btnSave;

    // 数据
    private CategoryAdapter categoryAdapter;
    private ClothesAdapter clothesAdapter;
    private ClothesAdapter selectedClothesAdapter;
    private List<Category> categories = new ArrayList<>();
    private List<Clothes> clothesList = new ArrayList<>();
    private List<Clothes> selectedClothesList = new ArrayList<>();
    private int selectedCategoryId = 0; // 默认选择"全部"分类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_match);

        // 检查用户是否已登录
        if (!LoginCheckUtil.checkLoginAndRedirect(this, true)) {
            finish();
            return;
        }

        initViews();
        setupToolbar();
        setupCategories();
        setupClothesRecyclerView();
        setupSelectedClothesRecyclerView();
        setupListeners();
        loadClothes();
    }

    private void initViews() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        etTitle = findViewById(R.id.etTitle);
        etDescription = findViewById(R.id.etDescription);
        rvCategories = findViewById(R.id.rvCategories);
        rvSelectedClothes = findViewById(R.id.rvSelectedClothes);
        rvClothes = findViewById(R.id.rvClothes);
        btnSave = findViewById(R.id.btnSave);
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("创建搭配");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    private void setupCategories() {
        // 初始化分类数据
        categories.add(new Category(0, "全部"));
        categories.add(new Category(1, "上衣"));
        categories.add(new Category(2, "裤子"));
        categories.add(new Category(3, "裙子"));
        categories.add(new Category(4, "外套"));
        categories.add(new Category(5, "鞋子"));
        categories.add(new Category(6, "配饰"));

        categoryAdapter = new CategoryAdapter(this, categories);
        rvCategories.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvCategories.setAdapter(categoryAdapter);

        categoryAdapter.setOnCategoryClickListener(category -> {
            selectedCategoryId = category.getId();
            loadClothes();
        });
    }

    private void setupClothesRecyclerView() {
        clothesAdapter = new ClothesAdapter(this, clothesList);
        rvClothes.setLayoutManager(new GridLayoutManager(this, 3));
        rvClothes.setAdapter(clothesAdapter);

        clothesAdapter.setOnClothesClickListener(clothes -> {
            // 添加到已选择的衣物列表
            if (!isClothesAlreadySelected(clothes)) {
                selectedClothesList.add(clothes);
                selectedClothesAdapter.notifyItemInserted(selectedClothesList.size() - 1);
            } else {
                Toast.makeText(this, "该衣物已添加", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupSelectedClothesRecyclerView() {
        selectedClothesAdapter = new ClothesAdapter(this, selectedClothesList);
        rvSelectedClothes.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvSelectedClothes.setAdapter(selectedClothesAdapter);

        selectedClothesAdapter.setOnClothesClickListener(clothes -> {
            // 从已选择的衣物列表中移除
            int position = selectedClothesList.indexOf(clothes);
            if (position != -1) {
                selectedClothesList.remove(position);
                selectedClothesAdapter.notifyItemRemoved(position);
            }
        });
    }

    private void setupListeners() {
        btnSave.setOnClickListener(v -> saveMatch());
    }

    private void loadClothes() {
        // 模拟获取衣物数据
        clothesList.clear();
        
        // 模拟数据
        List<Clothes> allClothes = getSimulatedClothes();
        
        // 根据分类过滤
        for (Clothes clothes : allClothes) {
            if (selectedCategoryId == 0 || clothes.getCategory().equals(getCategoryNameById(selectedCategoryId))) {
                clothesList.add(clothes);
            }
        }
        
        clothesAdapter.notifyDataSetChanged();
    }

    private List<Clothes> getSimulatedClothes() {
        List<Clothes> clothes = new ArrayList<>();
        
        // 模拟数据
        clothes.add(new Clothes(1, "白色T恤", "上衣", "https://example.com/tshirt1.jpg", "春夏"));
        clothes.add(new Clothes(2, "黑色T恤", "上衣", "https://example.com/tshirt2.jpg", "四季"));
        clothes.add(new Clothes(3, "蓝色牛仔裤", "裤子", "https://example.com/jeans1.jpg", "四季"));
        clothes.add(new Clothes(4, "黑色牛仔裤", "裤子", "https://example.com/jeans2.jpg", "四季"));
        clothes.add(new Clothes(5, "红色连衣裙", "裙子", "https://example.com/dress1.jpg", "夏季"));
        clothes.add(new Clothes(6, "蓝色衬衫", "上衣", "https://example.com/shirt1.jpg", "春秋"));
        clothes.add(new Clothes(7, "黑色皮鞋", "鞋子", "https://example.com/shoes1.jpg", "四季"));
        clothes.add(new Clothes(8, "白色运动鞋", "鞋子", "https://example.com/shoes2.jpg", "四季"));
        clothes.add(new Clothes(9, "棕色皮带", "配饰", "https://example.com/belt1.jpg", "四季"));
        clothes.add(new Clothes(10, "黑色外套", "外套", "https://example.com/coat1.jpg", "秋冬"));
        
        return clothes;
    }

    private boolean isClothesAlreadySelected(Clothes clothes) {
        for (Clothes selected : selectedClothesList) {
            if (selected.getId() == clothes.getId()) {
                return true;
            }
        }
        return false;
    }

    private String getCategoryNameById(int categoryId) {
        for (Category category : categories) {
            if (category.getId() == categoryId) {
                return category.getName();
            }
        }
        return "";
    }

    private void saveMatch() {
        String title = etTitle.getText().toString().trim();
        String description = etDescription.getText().toString().trim();
        
        if (title.isEmpty()) {
            Toast.makeText(this, "请输入搭配标题", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (selectedClothesList.isEmpty()) {
            Toast.makeText(this, "请至少选择一件衣物", Toast.LENGTH_SHORT).show();
            return;
        }
        
        // 模拟保存搭配
        Match match = new Match();
        match.setId(1); // 模拟ID
        match.setTitle(title);
        match.setDescription(description);
        match.setCategoryId(1); // 默认分类ID
        match.setCategoryName("日常"); // 默认分类名称
        
        // 添加选中的衣物
        for (Clothes clothes : selectedClothesList) {
            match.addClothes(clothes);
        }
        
        // 模拟保存成功
        Toast.makeText(this, "搭配创建成功", Toast.LENGTH_SHORT).show();
        
        // 返回到搭配列表页面
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 